package com.niuke;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * 12. 猜密码
 * 用了和 5. MELON的难题 相同的递归代码，使用不同的判断来得到结果
 * https://www.nowcoder.com/discuss/353150503548821504?sourceSSR=search
 */
public class NiukeMoni14 {

    public static int[] data;
    public static int minLength;
    public static List<List<Integer>> result = new ArrayList<>();

    public static int[] tempNums = null; // 记录选中数值
    public static int[] tempIndex = null; // 记录选中下标的

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String str = scanner.nextLine();
        minLength = scanner.nextInt();
        String[] strArr = str.split(",");
        data = new int[strArr.length];
        tempNums = new int[strArr.length];
        tempIndex = new int[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            data[i] = Integer.parseInt(strArr[i]);
            tempIndex[i] = -1;
        }
        Arrays.sort(data);

        dfs(0, 0);

        for (int i = 0; i < result.size(); i++) {
            System.out.println(Arrays.toString(result.get(i).toArray()));
        }
    }

    public static void dfs(int p, int count) {
        System.out.println("p = " + p + ", count = " + count);
        if (count >= minLength) {
            List<Integer> item = new ArrayList<>();
            boolean needAdd = true;
            for (int i = 0; i < count; i++) {
                if (i > 0 && tempNums[i] < tempNums[i - 1]) {
                    needAdd = false;
                }
                item.add(tempNums[i]);
            }
            if (needAdd) {
                result.add(item);
            }
        }

        for (int i = 0; i < data.length; i++) {
            boolean flag = false;
            for (int j = 0; j < count; j++) {
                if (tempIndex[j] == i) {
                    flag = true;
                    break;
                }
            }
            System.out.println("flag = " + flag);
            if (!flag) {
                tempNums[count] = data[i];
                System.out.println(Arrays.toString(tempNums));
                tempIndex[count] = i;
                System.out.println(Arrays.toString(tempIndex));
                dfs(p + 1, count + 1);
            }
        }
    }

}
